﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SilentVoice.Models
{
    public partial class Group : Auditable
    {
        public Group()
        {
            this.Users = new HashSet<User>();
            this.Events = new HashSet<Event>();
        }

        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }

        //[Required]
        //public virtual Account Account { get; set; }
        public int AccountId { get; set; }

        public virtual ICollection<User> Users { get; set; }
        public virtual ICollection<Event> Events { get; set; }

        //TODO: business layer
        /*
        public IEnumerable<ValidationResult> IsValid(ValidationContext validationContext)
        {
            //Users should all be part of the group's account
            foreach (User user in Users)
            {
                if (!user.Account.Equals(Account))
                {
                    yield return new ValidationResult("Not all Users in this Group belong to this Groups Account!", new[] { "Users" });
                }
            }


            yield break;
        }*/

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType().DeclaringType != this.GetType().DeclaringType)
            {
                System.Console.WriteLine("they're not the same type, {0} {1}", obj.GetType().DeclaringType.ToString(), this.GetType().DeclaringType.ToString());
                return false;
            }

            Group other = (Group)obj;

            return this.Name.Equals(other.Name);
        }

        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }
    }
}